package utils

import (
	"time"
)

func GetLastMinuteUnix() int64 {
	now := time.Now()                       //获取当前时间
	t := now.Add(time.Minute * -1)          // 获取上一分钟时间
	lastM := t.Unix() - int64(now.Second()) // 上一分钟时间-上一分钟秒数
	return lastM
}

func GetAddDayTime(day int) time.Time {
	nowTime := time.Now()
	return nowTime.AddDate(0, 0, day)
}

func GetDayStart2End() (int64, int64) {
	t := time.Now()
	startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
	endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 59, t.Location()).Unix()
	return startTime, endTime
}

func OneDayRange2Unix(date string) (int64, int64) {
	//当前时区
	loc, _ := time.LoadLocation("Local")
	startDate := date + " 00:00:00"
	startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)

	endDate := date + " 23:59:59"
	end, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)

	//返回当天0点和23点59分的时间戳
	return startTime.Unix(), end.Unix()
}

// 获取周本一
func WeekStart() time.Time {
	now := time.Now()

	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6
	}

	return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
}

// 本月1号
func MonthStart() time.Time {
	now := time.Now()
	currentYear, currentMonth, _ := now.Date()
	currentLocation := now.Location()
	//timehelper.Time格式
	return time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
}

// 获取某月的开始和结束时间mon为0本月,-1上月，1下月以此类推
func GetLastDayOfMonth(mon int) time.Time {
	year, month, _ := time.Now().Date()
	thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)

	return thisMonth.AddDate(0, mon+1, -1)
}

// 获取某周的开始和结束时间,week为0本周,-1上周，1下周以此类推
func GetLastDayOfWeek(week int) time.Time {
	now := time.Now()
	offset := int(time.Monday - now.Weekday())
	//周日做特殊判断 因为time.Monday = 0
	if offset > 0 {
		offset = -6
	}

	year, month, day := now.Date()
	thisWeek := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
	return thisWeek.AddDate(0, 0, offset+6+7*week)
}
